package P3087;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * Problema da disciplina de Estrutura de Dados e Complexidade de Algoritmos da
 * Pos Graduacao do Periodo 2011.1
 *
 * @see http://br.spoj.pl/problems/JASPION/
 * @author Elenilson Vieira - 111100371 - elenilson[at]elenilsonvieira.com
 * @since 11 de maio de 2011
 */
public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        
        int instances = Integer.parseInt(reader.readLine());
        
        for (int i = 0; i < instances; i++) {
            Map<String, String> dictionary = new HashMap<String, String>();

            String data[] = reader.readLine().split(" ");
            int numberOfWords = Integer.parseInt(data[0]);
            int numberOfLines = Integer.parseInt(data[1]);

            for (int j= 0; j < numberOfWords; j++) {
                String japaneseWord = reader.readLine();
                String portugueseWord = reader.readLine();

                dictionary.put(japaneseWord, portugueseWord);
            }

            for (int j= 0; j < numberOfLines; j++) {
                String japaneseLine[] = reader.readLine().split(" ");
                StringBuilder portugueseLine = new StringBuilder();

                for (String japaneseWord : japaneseLine) {
                    String portugueseWord = dictionary.get(japaneseWord);

                    portugueseLine.append(portugueseWord != null ? portugueseWord : japaneseWord).append(" ");
                }

                System.out.println(portugueseLine.toString().trim());
            }
        }
    }

}
